package com.xu.book.servlet;

import com.alibaba.fastjson.JSONArray;
import com.xu.book.bean.Classes;
import com.xu.book.bean.Depart;
import com.xu.book.service.IClassesService;
import com.xu.book.service.IDepartService;
import com.xu.book.service.impl.ClassesServiceimpl;
import com.xu.book.service.impl.DepartServiceimpl;
import com.xu.sys.servlet.BaseServlet;
import com.xu.sys.utils.Constant;
import com.xu.sys.utils.StringUtils;
import sun.awt.LightweightFrame;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "classesServlet",urlPatterns = {"/book/classesServlet"})
public class ClassesServlet extends BaseServlet {
    private IClassesService service = new ClassesServiceimpl();
    private IDepartService departService = new DepartServiceimpl();
    @Override
    public void list(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        super.list(req, resp);
        service.listPage(pageUtils);
        req.setAttribute(Constant.LIST_PAGE_UTILS,pageUtils);
        req.getRequestDispatcher("/book/classes/list.jsp").forward(req,resp);

    }

    @Override
    public void saveOrUpdatePage(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String id =req.getParameter("id");
        if (StringUtils.isNotEmpty(id)){
            //更新
            Classes entity = service.findById(Integer.parseInt(id));
            req.setAttribute("entity",entity);
        }
        //需要分配院系，查询所有的院系信息
        List<Depart> departs = departService.list();
        req.setAttribute("departs",departs);
        req.getRequestDispatcher("/book/classes/addOrUpdate.jsp").forward(req,resp);
    }

    @Override
    public void saveOrUpdate(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String notes = req.getParameter("notes");
        String departId = req.getParameter("departId");
        Classes cls = new Classes();
        cls.setName(name);
        cls.setNotes(notes);
        if (StringUtils.isNotEmpty(departId)){
            //说有已经分配部门
            Depart depart = departService.findById(Integer.parseInt(departId));
            cls.setDepartId(Integer.parseInt(departId));
            cls.setDepartName(depart.getName());
        }
        if (StringUtils.isNotEmpty(id)){
            //更新
            cls.setId(Integer.parseInt(id));
            service.updateById(cls);
        }else{
            //添加
            service.save(cls);
        }
        resp.sendRedirect("/book/classesServlet?action=list");

    }



    @Override
    public void remove(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String id = req.getParameter("id");
        service.deleteById(Integer.parseInt(id));
        //ajax 异步响应
        PrintWriter writer = resp.getWriter();
        writer.write("ok");
        writer.flush();
    }

    @Override
    public void findById(HttpServletRequest req, HttpServletResponse resp) throws Exception {

    }

    public void findByDepartId(HttpServletRequest req,HttpServletResponse resp) throws Exception{
        //获取传递的部门编号
        String departId = req.getParameter("departId");
        Classes cls = new Classes();
        cls.setDepartId(Integer.parseInt(departId));
        List<Classes> list = service.list(cls);
        //把list集合对象转换成为json对象字符串响应传递给前端
        String json = JSONArray.toJSONString(list);
        //设置字符编码
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.write(json);
        writer.flush();

    }
}
